<template>
 <div  class="components-container">
        <sweet-modal ref="dialog" class="dialog">
            <h3>{{ title }}</h3>
             <div style=" height:200px; margin:30px 50px">

              <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label">部门名称</label>
                  <div class="layui-input-inline">
                    <el-input v-model="dptData.DPTNAME"   placeholder="请输入部门名称"></el-input>
                  </div>

                  <label class="layui-form-label">部门编号</label>
                  <div class="layui-input-inline">
                    <el-input v-model="dptData.DPTNO"   placeholder="请输入部门编号"></el-input>
                  </div>
                </div>
              </div>
              <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label">联系人</label>
                  <div class="layui-input-inline">
                    <el-input v-model="dptData.DPTPSN"  placeholder="请输入联系人" ></el-input>
                  </div>
                   <label class="layui-form-label">联系电话</label>
                  <div class="layui-input-inline">
                    <el-input  v-model="dptData.DPTTEL"   placeholder="请输入联系人电话"></el-input>
                  </div>
                </div>
              </div>
              <div class="layui-form-item"  v-if="showxzc">
                <div class="layui-inline">
                  <label class="layui-form-label">管辖范围</label>
                    <div class="layui-input-inline">
                      <el-cascader  style="width:480px"  class="dz"  ref="address"  v-model="arr" :options="addressOptions" change-on-select>
                      </el-cascader>    
                    </div>
                </div>
              </div>
             </div>
            <div class="layui-form-item btnCenter">
              <el-button @click="closeDialog">取消</el-button>
              <el-button type="primary" @click="submit" :loading="loading">确定</el-button>
            </div>
        </sweet-modal>
<!-- 详情 -->
         <sweet-modal ref="dialogInfo" class="dialog">
              <h3>{{ title }}</h3>
             <div style=" height:200px; margin:30px 50px">

              <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label">部门名称</label>
                  <div class="layui-input-inline">
                    <el-input v-model="dptData.DPTNAME"  readonly></el-input>
                  </div>

                  <label class="layui-form-label">部门编号</label>
                  <div class="layui-input-inline">
                    <el-input v-model="dptData.DPTNO"   readonly></el-input>
                  </div>
                </div>
              </div>
              <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label">联系人</label>
                  <div class="layui-input-inline">
                    <el-input v-model="dptData.DPTPSN"  readonly></el-input>
                  </div>
                   <label class="layui-form-label">联系电话</label>
                  <div class="layui-input-inline">
                    <el-input  v-model="dptData.DPTTEL"  readonly></el-input>
                  </div>
                </div>
              </div>
               <div class="layui-form-item"  v-if="showxzc">
                <div class="layui-inline">
                  <label class="layui-form-label">管辖范围</label>
                    <div class="layui-input-inline">
                      <el-input  v-model="dptData.DeceasSarIdName"  readonly></el-input>
                    </div>
                </div>
              </div>
             </div>

            <div class="layui-form-item btnCenter">
              <el-button @click="closeDialog">取消</el-button>
            </div>
         </sweet-modal>
 </div>
</template>

<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
export default {
  name: "DepartmentMain",
  data() {
    return {
      title: null,
      states: [],
      options: [],
      loading: false,
      dptData: {},
      showxzc:null,
      arr: [],
      addressOptions: [],
    };
  },
  methods: {
    //功能操作
    open(order, id) {
      this.order = order;
      this.dataRepeat();
      switch (this.order) {
        case ADD:
          this.title = "新增";

          this.$refs.dialog.open();
          break;
        case MODIFY:
          this.title = "修改";

          this.dataInfo(id);
          this.$refs.dialog.open();
          break;
        case INFO:
          this.title = "详情";
          this.dataInfo(id);
          this.$refs.dialogInfo.open();
          break;
        case DELETE:
          this.dataDelete(id);
          break;
        default:
      }
    },
    //提交
    submit() {
      switch (this.order) {
        case ADD:
          this.dataAdd();
          break;
        case MODIFY:
          this.dataModify();
          break;
        case DELETE:
          break;
        case INFO:
          this.$refs.dialogInfo.close();
          break;
        default:
      }
    },

    //数据重置
    dataRepeat() {
      this.dptData = {};
      this.arr=[];
    },
    //新增提交
    dataAdd() {
      this.loading = true;
      let Data = JSON.parse(JSON.stringify(this.dptData));
      Data.DPTDTRCODE = this.arr[this.arr.length - 1];
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Dept.Create,
        res => {
          this.loading = false;
          this.$refs.dialog.close();

          this.$message({
            type: "success",
            message: "新增成功!"
          });
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
      );
    },
    // 删除表格列
    dataDelete(id) {
      this.dptData.DPTID = id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            DPTID: id
          };
          apiBase.DoService(data, apiBase.getApiAdress().Dept.Delete, res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.refreshData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //修改提交
    dataModify() {
      this.loading = true;
      let Data = JSON.parse(JSON.stringify(this.dptData));
      Data.DPTDTRCODE = this.arr[this.arr.length - 1];
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Dept.Modify,
        res => {
          this.loading = false;
          this.$refs.dialog.close();
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
      );
    },

    //详情
    dataInfo(id) {
      let data = {
        DPTID: id
      };
      apiBase.DoService(data, apiBase.getApiAdress().Dept.Detail, res => {
        this.dptData = res.data.content;
        this.arr = res.data.content.DeceasSarId.split(",");
      });
    },
    closeDialog() {
      if (this.order === ADD || this.order === MODIFY)
        this.$refs.dialog.close();
      else if (this.order === INFO) this.$refs.dialogInfo.close();
    },

    refreshData() {
      this.$emit("refreshData");
    },
    //获取地址
    restructur(value) {
      let arr = JSON.parse(JSON.stringify(value));
      var data = [];
      arr.forEach(item => {
        if (item.Children.length > 0) {
          data.push({
            label: item.SARNAME,
            value: item.SARID,
            level: item.SARLEVEL,
            children: this.restructur(item.Children)
          });
        } else {
          data.push({
            label: item.SARNAME,
            value: item.SARID,
            level: item.SARLEVEL
          });
        }
      });
      return data;
    },
  },
  components: {
    sweetModal
  },
    activated() {
    //控制地址下拉显隐
      apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsBySar, res => {
      let show = res.data.content;
      this.showxzc = show;
      });
    // 获取级联选择
      apiBase.DoService({}, apiBase.getApiAdress().Code.GetBoroughByCode, res => {
        let data = res.data.content;
        this.addressOptions = data.map(item => ({
          label: item.SARNAME,
          value: item.SARID,
          level: item.SARLEVEL,
          children: item.Children.map(item => ({
            label: item.SARNAME,
            value: item.SARID,
            level: item.SARLEVEL,
            children: this.restructur(item.Children)
          }))
        }));
      });
    }
};

</script>

<style>

</style>
